/*
 *  Copyright 2008-2009 NVIDIA Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/*! \file reorder.h
 *  \brief Reorder a graph
 */

#pragma once

#include <cusp/detail/config.h>

#include <cusp/array1d.h>
#include <cusp/csr_matrix.h>
#include <cusp/blas.h>

#include <thrust/iterator/constant_iterator.h>

namespace cusp
{
namespace graph
{

template< typename IndexType, typename ValueType>
void reorder(const cusp::csr_matrix<IndexType,ValueType,cusp::host_memory>& A,
                 cusp::array1d<IndexType,cusp::host_memory>& perm,
                 cusp::array1d<IndexType,cusp::host_memory>& iperm)
{
    typedef cusp::array1d<IndexType,cusp::host_memory> ArrayType;

    /* Minimum Degree */
    IndexType mxint  = (1<<(8*sizeof(IndexType)-2));
    IndexType n = A.num_rows;
    IndexType nnz = A.num_entries;
    IndexType delta = 1;
    IndexType nzout;

    ArrayType head(n);
    ArrayType qsize(n);
    ArrayType Nlist(n);
    ArrayType marker(n);
    ArrayType row_offsets(A.row_offsets);
    ArrayType column_indices(A.column_indices);

    perm.resize(n);
    iperm.resize(n);

    typedef typename cusp::array1d_view< thrust::constant_iterator<IndexType> > ConstantView;
    ConstantView constant_view_n1(thrust::constant_iterator<IndexType>(1),
                               thrust::constant_iterator<IndexType>(1)+(n+1));
    cusp::blas::axpy(constant_view_n1, row_offsets, IndexType(1));

    ConstantView constant_view_nnz(thrust::constant_iterator<IndexType>(1),
                               thrust::constant_iterator<IndexType>(1)+nnz);
    cusp::blas::axpy(constant_view_nnz, column_indices, IndexType(1));

    genmmd( n,
            thrust::raw_pointer_cast(&row_offsets[0]),
            thrust::raw_pointer_cast(&column_indices[0]),
            thrust::raw_pointer_cast(&perm[0]),
            thrust::raw_pointer_cast(&iperm[0]),
            delta,
            thrust::raw_pointer_cast(&head[0]),
            thrust::raw_pointer_cast(&qsize[0]),
            thrust::raw_pointer_cast(&Nlist[0]),
            thrust::raw_pointer_cast(&marker[0]),
            mxint, &nzout);

    ConstantView constant_view_n(thrust::constant_iterator<IndexType>(1),
                               thrust::constant_iterator<IndexType>(1)+n);
    cusp::blas::axpy(constant_view_n, perm, IndexType(-1));
    cusp::blas::axpy(constant_view_n, iperm, IndexType(-1));
}

template< typename IndexType, typename ValueType>
void reorder(const cusp::csr_matrix<IndexType,ValueType,cusp::device_memory>& A,
                 cusp::array1d<IndexType,cusp::device_memory>& perm,
                 cusp::array1d<IndexType,cusp::device_memory>& iperm)
{
    cusp::csr_matrix<IndexType,ValueType,cusp::host_memory> A_h(A);
    cusp::array1d<IndexType,cusp::host_memory> perm_h;
    cusp::array1d<IndexType,cusp::host_memory> iperm_h;

    reorder( A_h, perm_h, iperm_h );

    perm = perm_h;
    iperm = iperm_h;
}

} // end namespace graph
} // end namespace cusp

#include <cusp/graph/detail/reference/mmd.inl>
